/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function(head) {
  if (head == null) return head;

  for (let curr = head; curr != null && curr.next != null; curr = curr.next.next) {
    const cv = curr.val;
    curr.val = curr.next.val;
    curr.next.val = cv;
  }
  return head;
};

// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------

function ListNode(val, next = null) {
  this.val = val;
  this.next = next;
  this.toString = () => {
    const current = '' + this.val + ', ';
    const nextStr = this.next === null ? '' : this.next.toString();
    return current + nextStr;
  };
}

console.log(swapPairs(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))))).toString());
